﻿#region Copyrights Notice
//©2010 Microsoft Corporation. All rights reserved.
#endregion

#region usings
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.UI;
using System.Web;
using System.IO;
using System.Web.UI.WebControls;
using System.Configuration;
#endregion

namespace Microsoft.InformationSecurity.CISF.SecurityPortal.UI.WebLibrary
{
    public abstract class CISFBasePage : System.Web.UI.Page
    {
        protected override void OnInit(EventArgs e)
        {
            try
            {
                //*** Adding Application specific CSS and JS links from the App config values ***//
                //*** Following links will allow application developers to create their own application specific styles ***//
                string strApplicationFolderName = ConfigurationManager.AppSettings["ApplicationFolderName"].ToString();

                Literal linkCS = new Literal();
                Literal linkJS = new Literal();
                if (strApplicationFolderName != "")
                {
                    linkCS.Text = "<link href=\"/Applications/" + strApplicationFolderName + "/UI/Styles/Styles.css\" rel=\"stylesheet\" type=\"text/css\" />";
                    linkJS.Text = "<script src=\"/Applications/" + strApplicationFolderName + "/UI/Scripts/Scripts.js\" type=\"text/javascript\" ></script>";
                }
                else
                {
                    linkCS.Text = "<link href=\"/UI/Styles/Styles.css\" rel=\"stylesheet\" type=\"text/css\" />";
                    linkJS.Text = "<script src=\"/UI/Scripts/Scripts.js\" type=\"text/javascript\"></script>";
                }
                //adding JS link to the page header
                base.Page.Header.Controls.Add(linkJS);
                //adding CSS link to the page header
                base.Page.Header.Controls.Add(linkCS);
            }
            catch { } //do nothing

            base.OnInit(e);
        }

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);

            // Set the page title
            this.SetPageTitle();
        }

        //Implementing Base Page features here

        #region Set PageTitle
        protected virtual bool AutoSetPageTitle
        {
            get
            {
                object o = ViewState["AutoSetPageTitle"];
                if (o == null)
                    return true;
                else
                    return (bool)o;
            }
            set
            {
                ViewState["AutoSetPageTitle"] = value;
            }
        }

        protected virtual string SiteMapProvider
        {
            get
            {
                string str = ViewState["SiteMapProvider"] as string;
                if (str == null)
                    return string.Empty;
                else
                    return str;
            }
            set
            {
                ViewState["SiteMapProvider"] = value;
            }
        }

        protected virtual string SiteMapNodeSeparator
        {
            get
            {
                string str = ViewState["SiteMapNodeSeparator"] as string;
                if (str == null)
                    return " :: ";
                else
                    return str;
            }
            set
            {
                ViewState["SiteMapNodeSeparator"] = value;
            }
        }

        protected virtual void SetPageTitle()
        {
            try
            {
                if (this.AutoSetPageTitle && (string.IsNullOrEmpty(this.Title) || string.Compare(this.Title, "Untitled Page", true) == 0))
                {
                    if (SiteMap.Enabled)
                    {
                        // See if this page is in the SiteMap
                        SiteMapNode current = null;

                        if (string.IsNullOrEmpty(this.SiteMapProvider))
                            current = SiteMap.CurrentNode;
                        else
                            current = SiteMap.Providers[this.SiteMapProvider].CurrentNode;

                        if (current != null)
                        {
                            // Build up the title
                            //SetPageTitle(current);

                            //setting the title based on the OnePortal SharePoint Title
                            SetPageTitle(current, current.ParentNode);
                            return;
                        }
                    }

                    // If we reach here we still do not have a title set... use the filename
                    this.Title = ConfigurationManager.AppSettings["ApplicationName"].ToString() + " :: " + Path.GetFileNameWithoutExtension(Request.PhysicalPath);
                }
            }
            catch { } //exit gracefully
        }

        protected virtual void SetPageTitle(SiteMapNode current)
        {
            if (this.AutoSetPageTitle)
            {
                StringBuilder titleBuilder = new StringBuilder(200);
                titleBuilder.Append(current.Title);

                current = current.ParentNode;

                if (current != null)
                {
                    string parentPathReverse = current.Title;

                    while (current.ParentNode != null)
                    {
                        current = current.ParentNode;

                        parentPathReverse = string.Concat(current.Title, this.SiteMapNodeSeparator, parentPathReverse);
                    }

                    titleBuilder.Append(" (").Append(parentPathReverse).Append(")");
                }

                this.Title = titleBuilder.ToString();
            }
        }

        //Updated by Anil Chintala. Date: May 18th 2010
        //Based on the format required by the One Portal Page TITLE
        protected virtual void SetPageTitle(SiteMapNode current, SiteMapNode parent)
        {
            try
            {
                if (this.AutoSetPageTitle)
                {
                    StringBuilder titleBuilder = new StringBuilder(200);

                    if (parent != null)
                    {
                        if (parent.Title == ConfigurationManager.AppSettings["CISFAppRootTitle"].ToString())
                            titleBuilder.Append(ConfigurationManager.AppSettings["CISFRootAppName"].ToString() + " :: " + current.Title);
                        else if (parent.Title == ConfigurationManager.AppSettings["CISFAppDirectoryTitle"].ToString())
                            titleBuilder.Append(current.Title + " :: " + "Home");
                        else
                        {
                            string parentPathReverse = String.Empty;
                            if (current.Title != null)
                                parentPathReverse = current.Title;

                            if (parent.ParentNode != null)
                            {
                                while (parent.Title != ConfigurationManager.AppSettings["CISFAppDirectoryTitle"].ToString())
                                {
                                    parentPathReverse = string.Concat(parent.Title, this.SiteMapNodeSeparator, parentPathReverse);
                                    parent = parent.ParentNode;
                                }
                            }
                            else
                                parentPathReverse = string.Concat(parent.Title, this.SiteMapNodeSeparator, parentPathReverse);

                            //finally append to string builder
                            titleBuilder.Append(parentPathReverse);
                        }
                    }

                    this.Title = titleBuilder.ToString();
                }
            }
            catch { } //exit gracefully
        }

        #endregion
    }
}
